from conan import ConanFile
from conan.tools.files import copy, chdir
from conan.tools.scm import Git
import os

class B2Recipe(ConanFile):
    name = "b2"
    version = "5.2.1"
    user = "tools"
    channel = "stable"
    license = "BSL-1.0"
    author = "René Ferdinand Rivera Morell"
    url = "https://github.com/bfgroup/b2"
    description = "B2 makes it easy to build C++ projects, everywhere."
    topics = ("build-system", "C++", "boost")
    settings = "os", "arch"  # B2 是工具，无需指定编译器和构建类型
    generators = "AutotoolsToolchain"  # 选择正确的生成器

    def source(self):
        # Clone the repository
        git = Git(self)
        git.clone("ssh://git@172.16.57.28:2289/ads/thirdparty/b2.git", target=self.source_folder)

        # Switch to the desired branch (or tag) after cloning
        with chdir(self, self.source_folder):
            git.checkout("5.2.1")

        self.output.info("source ：self.source_folder  = " + self.source_folder)

    def build(self):
        # Run bootstrap to set up the build environment
        self.run("./bootstrap.sh")

        # Build and install b2 locally
        self.run("./b2")

    def package(self):
        # Copy the b2 binary to the package folder
        copy(self, "b2", dst=os.path.join(self.package_folder, "bin"), src=self.build_folder, keep_path=False)

    def package_info(self):
        # Add the bin folder to the PATH
        self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
